--#include "data\config\boss\BossBattleFieldConfig.lua" once
--[[BOSS的刷出、复活机制剥离出来，在CommonBoss.lua中实现
本文件只处理BOSS之家独特的功能
]]

BossBattleFieldRankColumn = 
{	
	{"name"},				--0，玩家Name
	{"serverId"},			--1，玩家ServerId(留空)
	{"job"},				--2，玩家职业
}

BossBattleFieldRank 		= Lang.Rank.BossBattleFieldRank				--BOSS战场伤害排行榜
BossBattleFieldLastRank 	= Lang.Rank.BossBattleFieldLastRank			--BOSS战场伤害排行榜前一次榜

function GmDoBossBattleField( sysarg, flag, param )
	print("GmDoBossBattleField, flag="..flag..", param="..param)
	if flag == 1 then
		if param == 0 then
			StopBossBattleField()
		else
			StartBossBattleField()
		end
	elseif flag == 2 then
		local actorId = Actor.getActorId( sysarg )
		local str = GetCommonRankColumnValueById(actorId, BossBattleFieldRank, 1) or 0
		print("GmDoTwelvePalaces, str="..str)
	end
end


--活动开启
function StartBossBattleField()
	ClearCommonRank( BossBattleFieldRank )		--清除排行榜

	local dVar = System.getDyanmicVar()
	dVar.bossBattleFieldBeginTime  = System.getCurrMiniTime()			--活动开启时间
	dVar.bossBattleFieldIdxKilled  = 0 									--已击杀第N个BOSS

	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback(NpcPtr, "StopBossBattleField")
	Actor.unregScriptCallback(NpcPtr, "OnBossBattleFieldLoop")

	Actor.regScriptTimer(NpcPtr, 0, BossBattleFieldCfg.activityTime*1000, BossBattleFieldCfg.activityTime*1000, 1, 
		"StopBossBattleField")
	Actor.regScriptTimer(NpcPtr, 0, 5000, 5000, -1, "OnBossBattleFieldLoop")

	ClearBossBattleFieldAllBoss() 			--清理BOSS
	TickBossBattleFieldPlayerOut()			--T出残留的玩家
	FreshBossBattleFieldBoss( 1 )
	System.broadcastTipmsg( Lang.ScriptTips.BossBattleField01, ttScreenCenter + ttChatWindow )	
end

--[[活动结束
此方法，由定时器触发
]]
function StopBossBattleField()
	local dVar = System.getDyanmicVar()
	dVar.bossBattleFieldBeginTime = nil
	local NpcPtr = System.getGlobalNpc()
	Actor.unregScriptCallback(NpcPtr, "StopBossBattleField")
	Actor.unregScriptCallback(NpcPtr, "OnBossBattleFieldLoop")

	local freshCfg = GetElemByOpenServerDay( BossBattleFieldCfg.freshBoss )		 --根据开服天数获取具体奖品
	if not freshCfg then
		return
	end

	local bossKilled = dVar.bossBattleFieldIdxKilled or 0
	--print("StopBossBattleField, bossKilled="..bossKilled..", hasBossNum="..#freshCfg.boss)
	if bossKilled < #freshCfg.boss then 				--BOSS尚未击杀完毕
		OnBossBattleFieldOver( 0 )
	end

	ClearCommonRank( BossBattleFieldRank )				--清除排行榜
	dVar.bossBattleFieldIdxKilled = nil
	ClearBossBattleFieldAllBoss() 						--清理BOSS
	TickBossBattleFieldPlayerOut()						--T出残留的玩家
end


function OnBossBattleFieldLoop( )
	--print("OnBossBattleFieldLoop...")
	BoardBossBattleFieldHurtRank()			--全场景广播排行榜数据
end


--踢出活动玩家
function TickBossBattleFieldPlayerOut()
	local playerList = LuaHelp.getSceneActorListById( BossBattleFieldCfg.sceneId ) or {}
	for i, player in ipairs( playerList or {} ) do
		ProcBossBattleField( player )
	end
end

function ClearBossBattleFieldAllBoss()
	for freshIdx, freshCfg in ipairs( BossBattleFieldCfg.freshBoss ) do
		for bossIdx, bossCfg in ipairs( freshCfg.boss ) do
			clearScenceMonster( bossCfg ) 		--清除BOSS
		end
	end
end

function ProcBossBattleField( sysarg )
	Actor.returnCity( sysarg )
end

--[[
注册一些事件
]]
function ResisterBossBattleFieldProc()
	--print("ResisterBossBattleFieldProc...")
	SceneExitDispatcher.register( BossBattleFieldCfg.sceneId, OnExitBossBattleField )
end

--[[
退出场景
注意：手工退出、下线、被T出，GM指令Move出都会触发
一部分面板在OnActorReturnCity( sysarg )中触发关闭
]]
function OnExitBossBattleField( sysarg, sceneId, fubenId )
	CloseActivityRightPanel( sysarg )				--关闭右侧面板
	CloseBoardPanel( sysarg )						--关闭公告牌面板
	CloseCommonRankPanel( sysarg, enCommonRankType_BossBattleField )		--关闭排行榜面板
end

--[[
是否在活动中
]]
function IsBossBattleFieldValidTime( )
	local now 	= System.getCurrMiniTime()
	local dVar 	= System.getDyanmicVar()
	if dVar.bossBattleFieldBeginTime and dVar.bossBattleFieldBeginTime > 0 and now-dVar.bossBattleFieldBeginTime >=0 and
		now-dVar.bossBattleFieldBeginTime < BossBattleFieldCfg.activityTime then
		return true
	end
	return false
end


function InitBossBattleFieldRank()
	--print("*****>>InitBossBattleFieldRank...")
	Rank.Init(BossBattleFieldRank, BossBattleFieldRank, BossBattleFieldRankColumn, 1000, 0)
end


function EnterBossBattleField( sysarg )
	--print("EnterBossBattleField...")
	if not IsBossBattleFieldValidTime() then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
		return
	end

	if IsInBossBattleField( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0032, ttFlyTip)
		return
	end

	if CommonEnterScence( sysarg, BossBattleFieldCfg, GameLog.clBossBattleFieldEnterFee, Lang.ScriptTips.BossBattleFieldLog01 ) then
		Actor.openDialogs( sysarg, enLinkBossBattleFieldAwardWnd2 )
		OpenBossBattleFieldPanel( sysarg )				--发送左侧面板数据
		SendBossBattleFieldHurtRank( sysarg )			--下发排行榜数据
	end
end

function OpenBossBattleFieldPanel( sysarg )
	if not IsInBossBattleField( sysarg ) then
		return
	end

	local activityType 	= 2 			--活动
	local activityId 	= enRightPanelActivity_BossBattleField
	local restTime 		= GetBossBattleFieldLastTime()
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	panelInfo.title 	= Lang.ScriptTips.BossBattleFieldName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime


	local hurtName1  = GetCommonRankColumnValueByIdx(1, BossBattleFieldRank, 0) or Lang.ScriptTips.COM0078
	if hurtName1 == 0 then		--暂无第1名
		hurtName1 = Lang.ScriptTips.COM0078
	end
	local hurtValue1 = GetCommonRankValueByIdx(1, BossBattleFieldRank) or 0

	local actorId = Actor.getActorId( sysarg )
	local myRankIdx, myRankValue = GetCommonRankIdxAndValueDataById( actorId, BossBattleFieldRank, BossBattleFieldCfg.battleRank.rankLimit )

	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont17, 1, hurtName1 )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont18, 1, hurtValue1 )
	if myRankIdx <= 0 then
		panelInfo.contents[3]	= Lang.ScriptTips.panelCont19
	else
		panelInfo.contents[3]	= string.format( Lang.ScriptTips.panelCont20, myRankIdx )
	end
	panelInfo.contents[4]	= string.format( Lang.ScriptTips.panelCont21, myRankValue )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	panelInfo.buttons[2]	= 4	

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end


function ChangeBossBattleFieldPanel( sysarg , hurtName1, hurtValue1 )
	if not IsInBossBattleField( sysarg ) then
		return
	end

	local actorId = Actor.getActorId( sysarg )
	local myRankIdx, myRankValue = GetCommonRankIdxAndValueDataById( actorId, BossBattleFieldRank, BossBattleFieldCfg.battleRank.rankLimit )

	local panelInfo 	= {}
	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont17, 1, hurtName1 )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont18, 1, hurtValue1 )
	if myRankIdx <= 0 then
		panelInfo.contents[3]	= Lang.ScriptTips.panelCont19
	else
		panelInfo.contents[3]	= string.format( Lang.ScriptTips.panelCont20, myRankIdx )
	end
	panelInfo.contents[4]	= string.format( Lang.ScriptTips.panelCont21, myRankValue )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	panelInfo.buttons[2]	= 4

	ChangeActivityRightPanel( sysarg, panelInfo )
end

function GetBossBattleFieldLastTime( )
	local dVar = System.getDyanmicVar()
	if not dVar.bossBattleFieldBeginTime then 		--活动未开始
		return 0
	end

	local now = System.getCurrMiniTime()	
	local lastTime = dVar.bossBattleFieldBeginTime + BossBattleFieldCfg.activityTime - now
	if lastTime > 0 then
		return lastTime
	end
	return 0
end


function FreshBossBattleFieldBoss( bossIdx, freshCfg )
	--print("FreshBossBattleFieldBoss, bossIdx="..bossIdx)
	if not freshCfg then
		freshCfg = GetElemByOpenServerDay( BossBattleFieldCfg.freshBoss )		 --根据开服天数获取具体奖品
	end
	if not freshCfg then
		return
	end

	local bossCfg = freshCfg.boss[bossIdx]
	if not bossCfg then
		return
	end
	FreshCommonMonsters( { bossCfg }, 0, false )
end


function OnBossBattleFieldBossHurted( sysarg, bossPtr, hurtValue )
	local bossId 	= Actor.getIntProperty( bossPtr, PROP_ENTITY_ID )
	local sceneId 	= Actor.getSceneId(bossPtr)
	--print("OnBossBattleFieldBossHurted, bossId="..bossId..", sceneId="..sceneId)
	if not IsInBossBattleFieldBySceneId( sceneId ) then
		return
	end

	local bossCfg, freshCfg = GetBossBattleFieldBossCfgById( bossId )
	if not bossCfg then
		return
	end
	ProcBossBattleFieldHurtRank( sysarg, hurtValue )
end

function IsInBossBattleFieldBySceneId( sceneId )
	return BossBattleFieldCfg.sceneId == sceneId
end

function IsInBossBattleField( sysarg )
	local sceneId = Actor.getSceneId( sysarg )
	return IsInBossBattleFieldBySceneId( sceneId )
end

function GetBossBattleFieldBossCfgById( bossId )
	for freshidx, freshCfg in ipairs( BossBattleFieldCfg.freshBoss ) do
		for bossIdx, bossCfg in ipairs( freshCfg.boss ) do
			if bossCfg.monsterId == bossId then
				return bossCfg, freshCfg
			end
		end
	end
	return nil, nil
end

--[[
刷新BOSS伤害排行榜
]]
function ProcBossBattleFieldHurtRank( sysarg, addHurtValue )
	local actorId 	= Actor.getActorId( sysarg )
	--print("ProcBossBattleFieldHurtRank, actorId="..actorId..", addHurtValue="..addHurtValue)
	local job 		= Actor.getIntProperty( sysarg, PROP_ACTOR_VOCATION )
	UpdateCommonRankValueById( actorId, BossBattleFieldRank, addHurtValue )				--积分累加
	SetCommonRankColumnValueById( actorId, BossBattleFieldRank, 0, 0,  Actor.getName(sysarg) )
	SetCommonRankColumnValueById( actorId, BossBattleFieldRank, 0, 1,  0 )			--留空，无意义
	SetCommonRankColumnValueById( actorId, BossBattleFieldRank, 0, 2,  job )
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnBossBattleFieldBossDie( monster, sysarg, monId, lastHitKiller )
	local bossCfg, freshCfg = GetBossBattleFieldBossCfgById( monId )
	if not bossCfg then
		return
	end
	local dVar = System.getDyanmicVar()
	dVar.bossBattleFieldIdxKilled  = bossCfg.idx 		--已击杀第N个BOSS

	--print("OnBossBattleFieldBossDie, monId="..monId..", bossIdx="..bossCfg.idx..", bossNum="..#freshCfg.boss)
	if bossCfg.idx < #freshCfg.boss then 		--刷出下一个BOSS
		local NpcPtr = System.getGlobalNpc()
		Actor.unregScriptCallback(NpcPtr, "FreshBossBattleFieldNextBoss")
		Actor.regScriptTimer(NpcPtr, 0, BossBattleFieldCfg.freshBossCd*1000, BossBattleFieldCfg.freshBossCd*1000, 1, 
				"FreshBossBattleFieldNextBoss", bossCfg.idx)

		local boardStr = Lang.ScriptTips.BossBattleField02				--注意：字符串中要带有一个 %d
		OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, BossBattleFieldCfg.freshBossCd )
	else 		--击杀完毕
		OnBossBattleFieldOver( 1 )
	end
end


function FreshBossBattleFieldNextBoss( NpcPtr, bossIdx )
	bossIdx = tonumber(bossIdx)
	FreshBossBattleFieldBoss( bossIdx+1 )		--刷出下一只BOSS
end

function OnBossBattleFieldOver( sts )
	--print("OnBossBattleFieldOver, sts="..sts)
	if sts > 0 then			--全部BOSS击杀完毕
		System.broadcastTipmsg( Lang.ScriptTips.BossBattleField03, ttScreenCenter + ttChatWindow )
		--发放排名奖励和参与奖励
		--SendCommonRankAwards( enCommonRankType_BossBattleField, BossBattleFieldCfg.battleRank, BossBattleFieldCfg.battleJoinAward )
		SendCommonRankAwards( enCommonRankType_BossBattleField, BossBattleFieldCfg.battleRank )

		local NpcPtr = System.getGlobalNpc()
		Actor.unregScriptCallback(NpcPtr, "OnBossBattleFieldLoop")

		BoardBossBattleFieldHurtRank()			--全场景广播排行榜数据
	else 		--活动结束，没有击杀完毕
		System.broadcastTipmsg( Lang.ScriptTips.BossBattleField04, ttScreenCenter + ttChatWindow )
	end
end


function SendBossBattleFieldHurtRank( sysarg )
	local hurtName1  = GetCommonRankColumnValueByIdx(1, BossBattleFieldRank, 0) or Lang.ScriptTips.COM0078		--第1名名称
	if hurtName1 == 0 then		--暂无第1名
		hurtName1 = Lang.ScriptTips.COM0078
	end
	local hurtValue1 = GetCommonRankValueByIdx(1, BossBattleFieldRank) or 0 					--第1名伤害
	NotifyBossBattleFieldHurtRank( sysarg, hurtName1, hurtValue1 )
end

--[[
全场景同步BOSS伤害数据
]]
function BoardBossBattleFieldHurtRank()
	local hurtName1  = GetCommonRankColumnValueByIdx(1, BossBattleFieldRank, 0) or Lang.ScriptTips.COM0078		--第1名名称
	if hurtName1 == 0 then		--暂无第1名
		hurtName1 = Lang.ScriptTips.COM0078
	end
	local hurtValue1 = GetCommonRankValueByIdx(1, BossBattleFieldRank) or 0 					--第1名伤害
	local playerList = LuaHelp.getSceneActorListById( BossBattleFieldCfg.sceneId ) or {}
	for i, player in ipairs( playerList or {} ) do
		NotifyBossBattleFieldHurtRank( player, hurtName1, hurtValue1 )
	end
end

function NotifyBossBattleFieldHurtRank( player, hurtName1, hurtValue1 )
	ChangeBossBattleFieldPanel( player, hurtName1, hurtValue1 )
	--SendCommonRankSimpleData( player, enCommonRankType_BossBattleField, BossBattleFieldCfg.battleRank )
	SendCommonRankSimpleData( player, enCommonRankType_BossBattleField)
end

InitBossBattleFieldRank()
ResisterBossBattleFieldProc()				--注册一些事件

